package mapper;

import com.sqsf.javabean.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

public interface UserMapper {
    @Select("select * from user")
    List<User> selectAll();
    @Select(" select * from user where username=#{username} and password=#{password}")
    List<User> selectByUsernameAndPassword(@Param("username") String username, @Param("password") String password);

    @Select("select * from user where username=#{username}")
    List<User> selectByUsername(String username);
    @Delete("delete from user where uid=#{uid}")
    int deleteByUid(int uid);
    @Update("update user set password=#{password} where uid=#{uid}")
    int updatePasswordByUid(@Param("uid")int uid,@Param("password")String password);
    @Insert("insert into user(username,password,phone)values(#{username},#{password},#{phone})")
    int insertUser(User user);

    @Select("select uid,username uname,password pwd,phone tel from user")
    @Results(id = "u",value = {
            @Result(id = true,column = "uid",property = "uid"),
            @Result(column = "uname",property = "username"),
            @Result(column = "pwd",property = "password"),
            @Result(column = "tel",property = "phone")
    })
    List<User> selectUser();


    @Select("select uid,username uname,password pwd,phone tel from user")
    @ResultMap("u")
    List<User> selectUser1();

    @Select("select * from user")
    @Results(id = "u_r",value = {
            @Result(id = true,column = "uid",property = "uid"),
            @Result(property="r",column="uid",one = @One(
                    select="mapper.RoleMapper.selectByUid",
                    fetchType = FetchType.LAZY
            ))
    })
    List<User> selectUser_Role();

    @Delete({"<script>        delete from user where uid in\n" +
            "        <foreach collection=\"array\" item=\"uid\" open=\"(\" close=\")\" separator=\",\">\n" +
            "            #{uid}\n" +
            "        </foreach></script>"})
    int deleteByArray(int[] array);
}
